// JavaScript Document
Validator = {
	isNull:function(el){
		return el.value.length==0;
	},
	required:function(el){
		if(el.type=="checkbox"){
			valid = el.checked;
		}else{
			valid = !Validator.isNull(el);
		}
		return valid;
	},
	number:function(el){
		return (!isNaN(el.value) || this.isNull(el));
	},
	compare:function(el,withel){
		return (el.value==$(withel).value);
	},
	minlen:function(el,len){
		return (el.value.length>=len);
	},
	email:function(el){
		//TODO: CORREGIR LA EXPRESION REGULAR
		el.value = el.value.trim();
		var f = /^[0-9a-z_\-\.]*[0-9a-z_\-]@([0-9a-z_\-]+\.)+[a-z]{2,4}$/i;
		return (f.test(el.value) && !this.isNull(el));
	}
}

function $validate(frm,callback){
	var fs = true;
	for(var i = 0; i<frm.elements.length;i++){
		var lmnt = frm.elements[i];
		var dov = lmnt.className;//lmnt.getAttribute("className");
		var ok = true;
		if(dov!=null){
			doValidation = dov.indexOf("validate");
			if(doValidation>-1){
				searchValidation = /validate\((.*?)\)/;
				validation = dov.match(searchValidation);
				var val = validation[1].split(",");
				for(var v=0; v<val.length; v++){
					var ps = val[v].split(":");
					if(ps.length>1){
						var method = ps[0];
						var param = ps[1];
						if(Validator[method] && ok)
							ok = Validator[method](lmnt,param);
					}else{
						var method = val[v];
						if(Validator[method] && ok)
							ok = Validator[method](lmnt);
					}
				}
				hasErrorClass = (dov.indexOf("reqError")>-1);
				//lmnt.parentNode.className = "reqError";
				if(ok){
					if(hasErrorClass){
						newClassDefinition = new Array();
						classArray = dov.split(" ");
						for(ca=0;ca<classArray.length;ca++){
							if(classArray[ca]!="reqError") newClassDefinition.push(classArray[ca]);
						}
						lmnt.className = newClassDefinition.join(" ");
					}
				}else{
					if(!hasErrorClass){
						lmnt.className += " reqError";
					}
				}
				//lmnt.className = (!ok) ? lmnt.className+" reqError" : "";
			}
		}
		if(fs) fs = ok;
	}
	if(!fs){
		//callback();
		//alert("debe completar los campos requeridos, fijese que la informacion sea la correcta.");
		/*$("errorContent").innerHTML = "All fields are required. Please try again.";
		//alert("All fields are required. Please try again.");
		showError();*/
	}
	return fs;
}